﻿using Sidvall.DataPortal;

namespace Sidvall.IdentityManager.Business
{
    public class ServerConnectionProvider : IServerConnectionProvider
    {
        #region Public Members

        #region GetServerConnection

        public ServerConnection GetServerConnection(Sidvall.DataPortal.DataPortalStrategy dataPortalStrategy, Sidvall.Business.IDataPortalContext dataPortalContext, string context)
        {
            if (dataPortalStrategy?.MasterServerConnection != null)
                return dataPortalStrategy.MasterServerConnection;

            if (Sidvall.IdentityManager.SystemContext.Current.ActiveServerItem != null)
            {
                switch (Sidvall.IdentityManager.SystemContext.Current.ActiveServerItem.ConnectionTypeId)
                {
                    case Sidvall.IdentityManager.Data.ConnectionType.RemoteAccess:
                        return Sidvall.IdentityManager.SystemContext.Current.GetWebServiceConnection(Sidvall.IdentityManager.SystemContext.Current.ActiveServerItem.RemoteAddress, null);
                    default:
                        return Sidvall.IdentityManager.SystemContext.Current.GetSqlServerConnection(Sidvall.IdentityManager.SystemContext.Current.ActiveServerItem.ConnectionString, null);
                }
            }
            return null;
        }

        #endregion

        #endregion
    }
}
